package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.StatisVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }
    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        //dest
        strategyDetail.setDest(strategyService.get(strategyDetail.getStrategy().getId()).getDest());
        StrategyContent strategyContent = strategyDetail.getStrategyContent();
        //summary
        if(strategyContent.getContent().length() > 200){
            strategyDetail.setSummary(strategyContent.getContent().substring(0,200));
        }else{
            strategyDetail.setSummary(strategyContent.getContent());
        }
        //isabroad
        if(destinationService.getToasts(strategyDetail.getDest().getId()).get(0).getId() == 1L){
            strategyDetail.setIsabroad(false);
        }else{
            strategyDetail.setIsabroad(true);
        }
        if (strategyDetail.getId() == null){
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);
        }else{
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }
        //添加关系前需要先删除原有的关系
        strategyDetailMapper.deleteRelation(strategyDetail.getId());

        //标签的维护
        if(tags != null && !"".equals(tags.trim())){
            String[] split = tags.split(",");
            if(split != null && split.length > 0){
                for (String s : split) {
                    StrategyTag strategyTag = new StrategyTag();
                    strategyTag.setName(s);
                    strategyTagService.saveOrUpdate(strategyTag);
                    //维护标签明细中间表的关系
                    strategyDetailMapper.insertRelation(strategyDetail.getId(),strategyTag.getId());
                }
            }
        }
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyDetail> getViewnumTopThree(Long destId) {
        return strategyDetailMapper.getViewnumTopThree(destId);
    }

    @Override
    public void updateStatis(StatisVo statisVo) {
        strategyDetailMapper.updateStatis(statisVo);
    }
}
